<?php
/**
 * A helper class for debug
 */
class FileLog 
{
	private $f = null;
	static private $inst = null;
	private function __construct ($filepath="c:/habari.log")
	{
		if ($this->f != null) {
			fclose($this->f);
			$this->f = null;
		}

		$this->f = fopen($filepath, "a");
	}

	function __destruct ()
	{
		if ($this->f != null) {
			fclose($this->f);
			$this->f = null;
		}
	}

	static public function instance ()
	{
		if (self::$inst == null) {
			self::$inst = new FileLog();
		}

		return self::$inst;

	}

	public function write($string)
	{
		if (isset($this) == false) {
			return;
		}

		if (strrpos($string, "\n")+1 < strlen($string)) {
			$string = $string . "\n";
		}
		fwrite($this->f, $string);
	}

	public function flush()
	{
		if (isset($this) == false) {
			return;
		}

		fflush($this->f);
	}

	public function dump($var)
	{
		$dumpstr = var_export($var, true);
		$this->write("S##################################");
		$this->write($dumpstr);
		$this->write("E##################################");
	}
	public function bt()
	{
		$output = "S**********************************************";
		$debugid= md5(microtime());
		if(function_exists('debug_backtrace')) {
			$output = "";
			$backtrace = array_reverse(debug_backtrace(), true);
			$odd = '';
			$tracect = 0;
			foreach($backtrace as $trace) {
				$file = $line = $class = $type = $function = '';
				$args= array();
				extract($trace);
				if(isset($class))	$fname = $class . $type . $function; else	$fname = $function;
				if(!isset($file) || $file=='') $file = '[Internal PHP]'; else $file = basename($file);
				$output .= "{$file} : ({$line}):  {$fname}(";
				$comma = '';
				foreach((array)$args as $arg) {
					$tracect++;
					$argout = print_r($arg,1);
					$output .= $comma . Utils::debug_reveal( gettype($arg), htmlentities($argout), $debugid . $tracect, true );
					$comma = ', ';
				}
				$output .= ");\n";
			}

			$output .= "E**********************************************";
			$this->write($output);

		}
	}

}

?>

